/*
 * Fichier d'en-tête du projet menulib
 *
 * Cible : PSP
 * Auteur : BuLLeKeUp
 * Date de création : 17/03/2012
 * Date de dernière modification : 17/03/2012
 *
 * Ce projet est sous license GNU GPL v3. Consultez la license associée dans le repertoire doc/gplv3.txt
 * ou rendez vous sur http://www.gnu.org/copyleft/gpl.html
 *
 * Nom du fichier : ctrlHandler.cpp
 *
 * Description : Implémentations de la classe de gestion des touches
 *
 */

#include "ctrlHandler.h"

ctrlHandler::ctrlHandler() {
    // Pas de structure de controle, on annule la création de la classe
}

ctrlHandler::ctrlHandler(ctrlStruct *p_structCtrl) {
    /*if(p_structCtrl == NULL)
        break; //Interrompre la création de la classe*/
    
    this->structCtrl = p_structCtrl;
    this->getValuesFromClass();
    this->running = 1;
    this->pad = new SceCtrlData;
}

ctrlHandler::ctrlHandler(const ctrlHandler& orig) {
    this->structCtrl = orig.structCtrl;
    this->getValuesFromClass();
}

int ctrlHandler::getValuesFromClass() {
    switch(this->structCtrl->classType) {
        case MENU :
            this->optionSelected = (static_cast<menu *>(this->structCtrl->classPointer))->getOptionSelected();
            this->titleSelected = (static_cast<menu *>(this->structCtrl->classPointer))->getTitleSelected();
            this->titles = (static_cast<menu *>(this->structCtrl->classPointer))->getTitles();
            this->nbTitles = (static_cast<menu *>(this->structCtrl->classPointer))->getNbTitles();
            break;
        default :
            break;
    }
    
    return EXIT_SUCCESS;
}

ctrlStruct *ctrlHandler::getStructCtrl() {
    return this->structCtrl;
}

int ctrlHandler::runThreadMenu() {
    
    menu *currentMenu = (static_cast<menu *>(this->structCtrl->classPointer));
    
    while (this->running) {
        
        sceCtrlReadBufferPositive(pad, 1);
        
        optionSelected = currentMenu->getOptionSelected();
        titleSelected = currentMenu->getTitleSelected();
        
        if(pad->Buttons & PSP_CTRL_DOWN) {
            if(optionSelected < (titles[titleSelected].nbOptions-1))
                currentMenu->setOptionSelected(optionSelected+1);
            else if(titleSelected < (nbTitles-1)) {
                currentMenu->setTitleSelected(titleSelected+1);
                currentMenu->setOptionSelected(0);
            }
            currentMenu->printMenu();
            sceKernelDelayThread(500000);
        }
        
        if(pad->Buttons & PSP_CTRL_UP) {
            if(optionSelected > 0)
                currentMenu->setOptionSelected(optionSelected-1);
            else if(titleSelected > 0) {
                 currentMenu->setTitleSelected(titleSelected-1);
                currentMenu->setOptionSelected(titles[titleSelected-1].nbOptions-1);
            }
            currentMenu->printMenu();
            sceKernelDelayThread(500000);
        }
        
        if(pad->Buttons & PSP_CTRL_CROSS) {
            currentMenu->gotoOption();
            sceKernelDelayThread(500000);
        }
        
        sceKernelDelayThread(500);
    }
    
    return EXIT_SUCCESS;
}

int ctrlHandler::getRunningValue() {
    return this->running;
}

int ctrlHandler::setRunningValue(int p_value) {
    this->running = p_value;
    return EXIT_SUCCESS;
}

ctrlHandler::~ctrlHandler() {
}

